<template>
  <!--  页面跳转,表格数据也需要跳转-->
  <div style="padding:20px">
    <h3>编辑藏品信息</h3>
    <el-form :inline="true" :model="form" style="width:65%" :rules="rules" label-width="100px">
      <el-form-item label="藏品名" prop="name">
        <el-input v-model="form.name" placeholder="请输入藏品名"></el-input>
      </el-form-item>

      <el-form-item label="藏品介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" style="width:400px" placeholder="请输入藏品介绍内容"></el-input>
      </el-form-item>

      <el-form-item label="藏品图" prop="img">
        <el-input v-model="form.img" placeholder="请输入图片链接"></el-input>
      </el-form-item>

      <el-form-item label="藏品类别" prop="category">
        <el-cascader
            :props="{ value: 'name',label:'name' }"
            v-model="form.categories"
            :options="categories"
            @change="selCategory(form.categories)"
            placeholder="请选择类别"></el-cascader>
      </el-form-item>

      <el-form-item label="藏品类别" prop="categoryName">
        <el-input v-model="form.categoryName" placeholder="请输入藏品类别" disabled></el-input>
      </el-form-item>

      <el-form-item label="藏品类别ID" prop="categoryId">
        <el-input v-model="form.categoryId" placeholder="请输入藏品类别ID" disabled></el-input>
      </el-form-item>

      <el-form-item label="藏品年代" prop="era">
        <el-input v-model="form.era" placeholder="请输入藏品年代"></el-input>
      </el-form-item>

      <el-form-item label="藏品馆区" prop="area">
        <el-input v-model="form.area" placeholder="请输入藏品馆区"></el-input>
      </el-form-item>

      <el-form-item label="藏品国别" prop="nations">
        <el-input v-model="form.nations" placeholder="请输入藏品国别"></el-input>
      </el-form-item>
    </el-form>

    <div style="text-align:center;margin-top:30px">
      <el-button type="primary" @click="save" size="medium">提交</el-button>
      <!--      <el-button type="danger">返回列表</el-button>-->
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  name: "EditCollections",
  data(){
    return{
      form:{},
      categories:[],
      rules: {
        name: [
          {required: true, message: '请输入藏品名', trigger: 'blur'},
        ],
        introduce: [
          {required: true, message: '请输入藏品介绍', trigger: 'blur'},
        ],
        img: [
          {required: true, message: '请输入藏品图', trigger: 'blur'}
        ],
        categories: [
          {required: true, message: '请输入藏品类别', trigger: 'blur'}
        ],
        era:[
          {required: true, message: '请输入藏品年代', trigger: 'blur'}
        ],
        area:[
          {required: true, message: '请输入藏品馆区', trigger: 'blur'}
        ],
        nations:[
          {required: true, message: '请输入藏品国别', trigger: 'blur'}
        ]

      }
    }
  },
  created(){
    request.get('/collectionstype/tree').then(res => {
      this.categories = res.data
      // console.log("内容是",this.categories)
    })

    const id = this.$route.query.id
    //这个方法 将后台通过id查出来的collections对象赋给form
    request.get("/collections/"+ id).then(res => {
      //将后台查出的collections对象赋值给form
      this.form = res.data
      if(this.form.category){
        this.form.categories = this.form.category.split(' > ')
        console.log("categories内容是",this.form.categories)
      }
    })
  },
  methods: {
    save(){
      //  save方法用于提交数据
      request.put('/collections/update',this.form).then(res => {
        if (res.code === '200'){
          this.$notify.success('更新成功')
          //  新增成功后清空输入框内数据
          this.$router.push("/collectionsList")
        } else{
          this.$notify.error(res.msg)
        }

      })
    },
    selCategory(data){
      this.categories.forEach((item)=>{
        if(data[0] === item.name){
          item.children.forEach((pItem)=>{
            if(pItem.name === data[1]){
              this.form.categoryName = pItem.name
              this.form.categoryId = pItem.id
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>